package com.kong.meirimall.product.service.impl;

import cn.hutool.db.Page;
import com.kong.meirimall.product.entity.AttrEntity;
import com.kong.meirimall.product.service.AttrService;
import com.kong.meirimall.product.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kong.common.utils.PageUtils;
import com.kong.common.utils.Query;

import com.kong.meirimall.product.dao.AttrGroupDao;
import com.kong.meirimall.product.entity.AttrGroupEntity;
import com.kong.meirimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catalogId) {
        // 根据三级分类查
        //select * from pms_attr_group where catalog_id=? and (attr_group_id=key or attr_group_name like %key%)
        String key = (String)params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                // 条件拼装
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        // 如果传过来的三级分类 id 为 0 , 查所有
        if(catalogId==0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
            return new PageUtils(page);
        }else {
            wrapper.eq("catalog_id",catalogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsBycatalogId(Long catalogId) {
        //查出所有分组
        List<AttrGroupEntity> groupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catalog_id", catalogId));
        List<AttrGroupWithAttrsVo> groupWithAttrsVos = groupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo groupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, groupWithAttrsVo);
            //查出所有属性,将 vo 拼装完整
            List<AttrEntity> attrEntities = attrService.getRelationAttr(groupWithAttrsVo.getAttrGroupId());
            groupWithAttrsVo.setAttrs(attrEntities);
            return groupWithAttrsVo;
        }).collect(Collectors.toList());
        return groupWithAttrsVos;
    }

}